<?php

class PostsController extends BaseController
{
	public function postovi($kategorija)
	{
        $kat = Kategorija::where('naziv_rutiranje','=',$kategorija)->first();

        $vesti = DB::table('post')
                    ->join('vest', 'post.id', '=', 'vest.id')
                    ->join('kategorija', 'post.id_kategorija', '=', 'kategorija.id')
                    ->where('post.id_kategorija','=',$kat->id)
                    ->select('post.id', 'post.dim_h', 'post.dim_v', 'post.likes_num', 'post.tile_img_url','post.opis','post.tip',
                            'kategorija.boja', 'vest.naslov', 'vest.podnaslov')
                    ->get();

        $promos = DB::table('post')
            ->join('promo', 'post.id', '=', 'promo.id')
            ->join('kategorija', 'post.id_kategorija', '=', 'kategorija.id')
            ->where('post.id_kategorija','=',$kat->id)
            ->select('post.id', 'post.dim_h', 'post.dim_v', 'post.likes_num', 'post.tile_img_url','post.opis','post.tip',
                'kategorija.boja', 'promo.naslov', 'promo.podnaslov')
            ->get();

        $baneri = DB::table('post')
            ->join('baner', 'post.id', '=', 'baner.id')
            ->join('kategorija', 'post.id_kategorija', '=', 'kategorija.id')
            ->where('post.id_kategorija','=',$kat->id)
            ->select('post.id', 'post.dim_h', 'post.dim_v', 'post.likes_num', 'post.tile_img_url','post.opis','post.tip',
                'kategorija.boja')
            ->get();

        $postovi = array();

        foreach($vesti as $v)
        {
            $pt = new PostTileVM();
            $pt->id = $v->id;
            $pt->dim_v=$v->dim_v;
            $pt->dim_h = $v->dim_h;
            $pt->likes_num = $v->likes_num;
            $pt->img_url = 'upload/' . $v->tile_img_url;
            $pt->boja = $v->boja;
            $pt->naslov = $v->naslov;
            $pt->podnaslov = $v->podnaslov;
            $pt->opis = $v->opis;
            $pt->tip = $v->tip;
            $postovi[] = $pt;
        }
        foreach($promos as $v)
        {
            $pt = new PostTileVM();
            $pt->id = $v->id;
            $pt->dim_v=$v->dim_v;
            $pt->dim_h = $v->dim_h;
            $pt->likes_num = $v->likes_num;
            $pt->img_url = 'upload/' . $v->tile_img_url;
            $pt->boja = $v->boja;
            $pt->naslov = $v->naslov;
            $pt->podnaslov = $v->podnaslov;
            $pt->opis = $v->opis;
            $pt->tip = $v->tip;
            $postovi[] = $pt;
        }
        foreach($baneri as $v)
        {
            $pt = new PostTileVM();
            $pt->id = $v->id;
            $pt->dim_v=$v->dim_v;
            $pt->dim_h = $v->dim_h;
            $pt->tip = $v->tip;
            $pt->img_url = 'upload/' . $v->tile_img_url;
            $pt->boja = $v->boja;
            $pt->opis = $v->opis;
            $postovi[] = $pt;
        }

        return Response::json($postovi);
	}

    public function kategorije()
    {
        //$kategorije = Kategorija::select('id', 'naziv')->get();
        $kategorije = Kategorija::all();
        foreach($kategorije as $k)
            $k->img_url = 'img/' . $k->img_url;
        return Response::json($kategorije);
    }

    public function getPost($id)
    {
        $vest = DB::table('post')
                ->join('vest', 'post.id', '=', 'vest.id')
                ->join('kategorija', 'post.id_kategorija', '=', 'kategorija.id')
                ->join('autor', 'vest.id_autor', '=', 'autor.id')
                ->where('post.id', '=', $id)
                ->select('vest.naslov', 'vest.podnaslov', 'post.sadrzaj', 'post.img_url', 'kategorija.boja',
                    'post.likes_num', 'autor.opis')
                ->first();
        return $vest;
        /*$promo = DB::table('post')
            ->join('promo', 'post.id', '=', 'promo.id')
            ->join('kategorija', 'post.id_kategorija', '=', 'kategorija.id')
            ->where('post.id', '=', $id)
            ->select('promo.naslov', 'promo.podnaslov', 'post.sadrzaj', 'post.img_url', 'kategorija.boja',
                'post.likes_num')
            ->first();
        $rez = new VestDetailsVM();
        if($vest)
        {
            //$rez = new VestDetailsVM();
            $rez->naslov = $vest->naslov;
            $rez->podnaslov = $vest->podnaslov;
            $rez->sadrzaj = $vest->sadrzaj;
            $rez->img_url = 'upload/' . $vest->img_url;
            $rez->boja = $vest->boja;
            $rez->likes_num = $vest->likes_num;
            $rez->autor = $vest->opis;
        }
        if($promo)
        {
            //$rez = new VestDetailsVM();
            $rez->naslov = $promo->naslov;
            $rez->podnaslov = $promo->podnaslov;
            $rez->sadrzaj = $promo->sadrzaj;
            $rez->img_url = 'upload/' . $promo->img_url;
            $rez->boja = $promo->boja;
            $rez->likes_num = $promo->likes_num;
            $rez->autor = '';
        }
        //$rez=$vest;
        return Response::json($rez);*/
    }

    public  function unosVesti()
    {
        if(Session::get('tip') == 'admin' && Session::get('status_ulogovan') == true)
            return View::make('vesti.nova');
        else
            return View::make('admin.login');
    }

    public  function unosBanera()
    {
        if(Session::get('tip') == 'admin' && Session::get('status_ulogovan') == true)
            return View::make('baner.nova');
        else
            return View::make('admin.login');
    }

    public  function unosPromo()
    {
        if(Session::get('tip') == 'admin' && Session::get('status_ulogovan') == true)
            return View::make('promo.nova');
        else
            return View::make('admin.login');
    }



    public function snimiVest()
    {
        /*$fileTileImage = Input::file('tile_image');

        $destinationPath = 'upload/';
        $filename = $fileTileImage->getClientOriginalName();
        Input::file('tile_image')->move($destinationPath, $filename);
        //post_image
        $file = Input::file('tile_image');
        print_r('Slika: ' . $file);
        $destinationPath = 'upload/';
        $filename = $file->getClientOriginalName();
        Input::file('tile_image')->move($destinationPath, $filename);*/


        $response = array();

        try {
            $input = Input::all();

            $this->proveraPost($input);
            $this->proveraVest($input);

            $post =  $this->noviPost($input);
            $this->novaVest($input, $post);

            $post->tile_img_url = $this->uploadPostImage($post->id, 'tile', Input::file('tile_image'));
            $post->img_url =  $this->uploadPostImage($post->id, 'post', Input::file('post_image'));
            $post->save();

            $response['status']=true;
        }
        catch (Exception $e) {
            $response['status']=false;
            $response['poruka'] = $e->getMessage();
        }
        return Response::json($response);
    }

    private function uploadPostImage($id_post, $filenameStart, $file)
    {
        if($file==null)
            return;
        $destinationPath = 'upload/';
        $originalFileName = $file->getClientOriginalName();
        print_r($originalFileName);
        $ext = File::extension($originalFileName);

        $filename = $filenameStart . $id_post . '.' . $ext;
        print_r($filename);
        $file->move($destinationPath, $filename);
        return $filename;
    }

    private function proveraPost($input)
    {
        if(Util::isNull('opis', $input))
            throw new Exception('Morate uneti opis');
    }
    private function proveraVest($input)
    {
        if(Util::isNull('naslov', $input))
            throw new Exception('Morate uneti naslov');
    }

    //returns Post instance
    private function noviPost($input)
    {
        $post=new Post;
        $post->sadrzaj = $input['sadrzaj'];
        //$post->tile_img_url = $input['tile_img_url'];
        $post->opis = $input['opis'];
        $post->id_kategorija = $input['id_kategorija'];
        $post->tagovi = $input['tagovi'];
        $post->dim_h = $input['dim_h'];
        $post->dim_v = $input['dim_v'];
        $post->datum = new DateTime;
        $post->save();
        return $post;
    }

    private function novaVest($input, $post)
    {
        $vest = new Vest;
        $vest->post()->associate($post);

        $vest->naslov = $input['naslov'];
        $vest->podnaslov = $input['podnaslov'];
        $vest->id_autor = $input['id_autor'];
        $vest->save();
    }

}